package com.ruoyi.xqjh.lhxcgjh.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.infoRunTaskData.domain.InfoRunTaskData;
import com.ruoyi.infoRunTaskData.service.IInfoRunTaskDataService;
import com.ruoyi.xqjh.lhxcgjh.domain.Lhxcgjh;
import com.ruoyi.xqjh.lhxcgjh.domain.LhxcgjhMx;
import com.ruoyi.xqjh.lhxcgjh.mapper.LhxcgjhMapper;
import com.ruoyi.xqjh.lhxxqjh.domain.LhxxqjhMx;
import com.ruoyi.xqjh.lhxxqjh.service.ILhxxqjhService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.xqjh.lhxcgjh.service.ILhxcgjhService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.util.CollectionUtils;

import static com.ruoyi.common.utils.SecurityUtils.*;

/**
 * 6号线采购计划Service业务层处理
 *
 * @author huacai
 * @date 2025-04-16
 */
@Service
public class LhxcgjhServiceImpl implements ILhxcgjhService {
    @Autowired
    private LhxcgjhMapper lhxcgjhMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @Autowired
    private ILhxxqjhService lhxxqjhService;

    @Autowired
    private IInfoRunTaskDataService iInfoRunTaskDataService;

    /**
     * 查询6号线采购计划
     *
     * @param lhxcgjhId 6号线采购计划主键
     * @return 6号线采购计划
     */
    @Override
    public Lhxcgjh selectLhxcgjhByLhxcgjhId(String lhxcgjhId) {
        return lhxcgjhMapper.selectLhxcgjhByLhxcgjhId(lhxcgjhId);
    }

    /**
     * 查询6号线采购计划列表
     *
     * @param lhxcgjh 6号线采购计划
     * @return 6号线采购计划
     */
    @Override
    public List<Lhxcgjh> selectLhxcgjhList(Lhxcgjh lhxcgjh) {
        return lhxcgjhMapper.selectLhxcgjhList(lhxcgjh);
    }

    /**
     * 新增6号线采购计划
     *
     * @param lhxcgjh 6号线采购计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertLhxcgjh(Lhxcgjh lhxcgjh) {
        lhxcgjh.setCreateTime(DateUtils.getNowDate());
        lhxcgjh.setLhxcgjhId(IdUtils.fastSimpleUUID());
        lhxcgjh.setCreateBy(getUsername());
        lhxcgjh.setUserId(Math.toIntExact(getUserId()));
        lhxcgjh.setDeptId(Math.toIntExact(getDeptId()));
        int rows = lhxcgjhMapper.insertLhxcgjh(lhxcgjh);
        insertLhxcgjhMx(lhxcgjh);
        return rows;
    }

    /**
     * 批量新增6号线采购计划
     *
     * @param lhxcgjhs 6号线采购计划List
     * @return 结果
     */
    @Override
    public int batchInsertLhxcgjh(List<Lhxcgjh> lhxcgjhs) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(lhxcgjhs)) {
            try {
                for (int i = 0; i < lhxcgjhs.size(); i++) {
                    int row = lhxcgjhMapper.insertLhxcgjh(lhxcgjhs.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == lhxcgjhs.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改6号线采购计划
     *
     * @param lhxcgjh 6号线采购计划
     * @return 结果
     */
    @Transactional
    @Override
    public int updateLhxcgjh(Lhxcgjh lhxcgjh) {
        lhxcgjh.setUpdateTime(DateUtils.getNowDate());
        lhxcgjhMapper.deleteLhxcgjhMxByLhxcgjhId(lhxcgjh.getLhxcgjhId());
        insertLhxcgjhMx(lhxcgjh);
        return lhxcgjhMapper.updateLhxcgjh(lhxcgjh);
    }

    /**
     * 批量删除6号线采购计划
     *
     * @param lhxcgjhIds 需要删除的6号线采购计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteLhxcgjhByLhxcgjhIds(String[] lhxcgjhIds) {
        lhxcgjhMapper.deleteLhxcgjhMxByLhxcgjhIds(lhxcgjhIds);
        return lhxcgjhMapper.deleteLhxcgjhByLhxcgjhIds(lhxcgjhIds);
    }

    /**
     * 删除6号线采购计划信息
     *
     * @param lhxcgjhId 6号线采购计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteLhxcgjhByLhxcgjhId(String lhxcgjhId) {
        lhxcgjhMapper.deleteLhxcgjhMxByLhxcgjhId(lhxcgjhId);
        return lhxcgjhMapper.deleteLhxcgjhByLhxcgjhId(lhxcgjhId);
    }

    /**
     * 当6号线需求计划流程审批通过后
     * 将6号线需求计划主键ID传递过来
     * 并将6号线需求计划同步至6号线采购计划
     * lhxxqjhId: 6号线需求计划
     *
     * @param lhxxqjhId 6号线需求计划主键ID
     * @return 结束值
     */
    @Override
    public int getLhxxqjhIdByOss(String lhxxqjhId) {

        //根据6号线需求计划ID查询明细信息
        List<LhxxqjhMx> lhxxqjhMxList = lhxxqjhService.selectLhxxqjhByLhxxqjhId(lhxxqjhId).getLhxxqjhMxList();

        // 创建采购计划对象
        Lhxcgjh lhxcgjh = new Lhxcgjh();
        List<LhxcgjhMx> lhxcgjhMxList = new ArrayList<>(); // 需要初始化列表

        // 循环遍历每条需求计划明细信息
        for (LhxxqjhMx lhxxqjhMx : lhxxqjhMxList) {
            // 创建采购计划明细对象
            LhxcgjhMx lhxcgjhMx = new LhxcgjhMx();

            // 映射字段
            lhxcgjhMx.setLhxcgjhId(lhxxqjhId);
            lhxcgjhMx.setWzMc(lhxxqjhMx.getSbWzMc());       // 设备物资名称 -> 物资名称
            lhxcgjhMx.setGgJXh(lhxxqjhMx.getZyGgCs());      // 主要规格参数 -> 规格及型号
            lhxcgjhMx.setJsGyYq(lhxxqjhMx.getJsGyYq());     // 技术、工艺要求等 -> 技术、工艺要求等
            lhxcgjhMx.setSl(lhxxqjhMx.getSl());             // 数量 -> 数量
            lhxcgjhMx.setDw(lhxxqjhMx.getDw());             // 单位 -> 单位
            lhxcgjhMx.setSySj(lhxxqjhMx.getSySj());         // 使用时间 -> 使用时间
            lhxcgjhMx.setYtSm(lhxxqjhMx.getYtSm());         // 用途说明 -> 用途说明
            lhxcgjhMx.setBz(lhxxqjhMx.getBz());             // 备注 -> 备注

            // 将明细添加到列表中
            lhxcgjhMxList.add(lhxcgjhMx);
        }

        // 设置采购计划的明细列表
        lhxcgjh.setLhxcgjhMxList(lhxcgjhMxList);
        lhxcgjh.setCreateTime(DateUtils.getNowDate());
        lhxcgjh.setLhxcgjhId(lhxxqjhId);
        lhxcgjh.setCreateBy(getUsername());
        lhxcgjh.setUserId(Math.toIntExact(getUserId()));
        lhxcgjh.setDeptId(Math.toIntExact(getDeptId()));

        int rows = lhxcgjhMapper.insertLhxcgjh(lhxcgjh);
        insertLhxcgjhMx(lhxcgjh);

        //向流程业务表中加入数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        infoRunTaskData.setBusinessKey(lhxxqjhId);
        infoRunTaskData.setBusinessType(16); //业务模块: 6号线采购计划
        infoRunTaskData.setBusinessTitle("6号线采购计划"); //任务名称: 6号线采购计划
        infoRunTaskData.setApplyUser(SecurityUtils.getLoginUser().getUser().getNickName());//申请人
        iInfoRunTaskDataService.insertInfoRunTaskData(infoRunTaskData);

        return rows;
    }

    /**
     * 新增6号线采购计划明细信息
     *
     * @param lhxcgjh 6号线采购计划对象
     */
    public void insertLhxcgjhMx(Lhxcgjh lhxcgjh) {
        List<LhxcgjhMx> lhxcgjhMxList = lhxcgjh.getLhxcgjhMxList();
        String lhxcgjhId = lhxcgjh.getLhxcgjhId();
        if (StringUtils.isNotNull(lhxcgjhMxList)) {
            List<LhxcgjhMx> list = new ArrayList<LhxcgjhMx>();
            for (LhxcgjhMx lhxcgjhMx : lhxcgjhMxList) {
                lhxcgjhMx.setLhxcgjhId(lhxcgjhId);
                list.add(lhxcgjhMx);
            }
            if (list.size() > 0) {
                lhxcgjhMapper.batchLhxcgjhMx(list);
            }
        }
    }
}
